package com.ycp.erp.employee.ui.binder;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.view.multitytype.adapter.BaseItemBinder;
import com.one.common.view.multitytype.adapter.BaseViewHolderMulti;
import com.ycp.erp.R;
import com.ycp.erp.employee.model.bean.Employee;
import com.ycp.erp.view.HintDialog;

import java.util.concurrent.TimeUnit;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Class Des:
 * Created by bjh on 2021/9/16.
 */
public class EmployeeManagerBinder extends BaseItemBinder<Employee> {
    private final FragmentManager manager;

    public EmployeeManagerBinder(FragmentManager manager) {
        super(R.layout.item_employee_manager);
        this.manager=manager;
    }

    @SuppressLint("CheckResult")
    @Override
    protected void bindView(@NonNull BaseViewHolderMulti holder, @NonNull Employee item) {
        TextView  tvName=holder.getView(R.id.tvName);
        TextView  tvNamePY=holder.getView(R.id.tvNamePY);
        TextView  tvRoleName=holder.getView(R.id.tvRoleName);
        TextView  tvPhone=holder.getView(R.id.tvPhone);
        TextView  tvDate=holder.getView(R.id.tvDate);
        TextView  tvStatue=holder.getView(R.id.tvStatue);
        View  vVLine=holder.getView(R.id.vVLine);
        View  llTop=holder.getView(R.id.llTop);
        tvName.setText(item.getName());
        tvPhone.setText(item.getPhone());
        tvRoleName.setText(item.getRoleName());
        tvNamePY.setText(item.getNamePY());
        //表示已启用
        if (item.getStatue()==1){
            tvRoleName.setTextColor(mContext.getResources().getColor(R.color.root_blue));
            tvStatue.setSelected(true);
            tvStatue.setText("禁用");
            vVLine.setBackgroundColor(mContext.getResources().getColor(R.color.root_blue));
        }else {
            tvRoleName.setTextColor(mContext.getResources().getColor(R.color.color_c1c4c8));
            vVLine.setBackgroundColor(mContext.getResources().getColor(R.color.color_c1c4c8));
            tvStatue.setSelected(false);
            tvStatue.setText("启用");
        }
        RxView.clicks(tvStatue).throttleFirst(500, TimeUnit.MILLISECONDS).subscribe(o->{
            HintDialog hintDialog = new HintDialog();
            if (tvStatue.isSelected()){
                hintDialog.setHintMsg("提示","确认禁用[ "+item.getName()+" ]账号");
            }else {
                hintDialog.setHintMsg("提示","确认启用[ "+item.getName()+" ]账号");
            }

            hintDialog.setInputListener(new HintDialog.InputListener() {
                @Override
                public void onCancel() {

                }

                @Override
                public void onConfirm(String data) {
                    tvStatue.setSelected(!tvStatue.isSelected());
                    if (tvStatue.isSelected()){
                        item.setStatue(1);
                    }else {
                        item.setStatue(0);
                    }
                    if (item.getStatue()==1){
                        tvRoleName.setTextColor(mContext.getResources().getColor(R.color.root_blue));
                        vVLine.setBackgroundColor(mContext.getResources().getColor(R.color.root_blue));
                        tvStatue.setText("禁用");
                    }else {
                        tvRoleName.setTextColor(mContext.getResources().getColor(R.color.color_c1c4c8));
                        vVLine.setBackgroundColor(mContext.getResources().getColor(R.color.color_c1c4c8));
                        tvStatue.setText("启用");
                    }
                }
            });
            hintDialog.show(manager,"");
        });
        RxView.clicks(llTop).throttleFirst(500,TimeUnit.MILLISECONDS).subscribe(o->{
            RouterManager.getInstance().go(RouterPath.EMPLOYEE_DETAIL,new DefaultExtra(item));
        });
    }
}
